﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CRMClient.Models.Contact;

namespace CRMClient.Views.ContactViews
{
    public partial class ListContacts : PhoneApplicationPage
    {
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            App.ContactViewModel.load();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            App.ContactViewModel.saveOnCache();
        }

        public ListContacts()
        {
            InitializeComponent();
            DataContext = App.ContactViewModel;
        }

        private void appBarAddButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ContactViews/AddContact.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var listbox = (ListBox)sender;
            Response c = (Response)listbox.SelectedItem;
            string destination = "/Views/ContactViews/DetailsContact.xaml";
            if (c != null)
            {
                App.ContactViewModel.Contact = c;
                NavigationService.Navigate(new Uri(destination, UriKind.RelativeOrAbsolute));
            }
        }
    }
}